package com.caseProcess.utils;

import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class FileUpload {

    /*
     * @param file  上传的图片
     * @param path  上传相对路径 相对路径
     * return 集合 文件的相对路径和服务器完整地址的集合
     * */
    public static Map<String,String> upload(MultipartFile file, String path, HttpServletRequest httpServletRequest){
        Map<String,String> filePathSet=new HashMap<String, String>();
        /**
         * 项目服务器地址路径
         */
        String projectServerPath = httpServletRequest.getScheme() + "://"+httpServletRequest.getServerName()+":" +
                httpServletRequest.getServerPort() + httpServletRequest.getContextPath() + path;
        /**
         * 上传文件绝对路径
         */
        String projectServerRelativelyPath = httpServletRequest.getSession().getServletContext().getRealPath("/WEB-INF/classes"+path);
        //获取图片的原名称
        String oriName = file.getOriginalFilename();
        String fileName=UUIDUtil.getUUID2();
        String suffix=oriName.substring(oriName.lastIndexOf('.'));
        String result=fileName+suffix;
        //配置图片上传路径，注意此处的路径和MyMvcConfig.java中配置的路径保持一致
        File uploadPathFile = new File(projectServerRelativelyPath);
        //判断上传的文件夹是否存在
        if (!uploadPathFile.exists()) {
            //不存在则递归创建
            uploadPathFile.mkdirs();
        }
        try {
            // 构建真实的文件路径
            File newFile = new File(projectServerRelativelyPath + File.separator + result);
//            System.out.println(newFile.getAbsolutePath());
            // 上传图片到 -》 “绝对路径”
            file.transferTo(newFile);
//            System.out.println("上传成功");
        } catch (IOException e) {
            e.printStackTrace();
        }
        String fileServerPath = projectServerPath + result;
        filePathSet.put("fileRelativePath",path+result);
        filePathSet.put("fileServerPath",fileServerPath);
        filePathSet.put("fileType",suffix);
        return filePathSet;
    }


    /**
     *
     * @param files 上传的文件集
     * @param path 上传相对路径 相对路径
     * @param httpServletRequest
     * @return list 文件的相对路径和服务器完整地址的集合
     */
    public static List<Map<String,String>> upload(MultipartFile[] files, String path, HttpServletRequest httpServletRequest){
        List<Map<String,String>> list=new ArrayList<Map<String, String>>();
        Map<String,String> filePathSet=new HashMap<String, String>();
        /**
         * 项目服务器地址路径
         */
        String projectServerPath = httpServletRequest.getScheme() + "://"+httpServletRequest.getServerName()+":" +
                httpServletRequest.getServerPort() + httpServletRequest.getContextPath() + path;
        /**
         * 上传文件绝对路径
         */
        String projectServerRelativelyPath = httpServletRequest.getSession().getServletContext().getRealPath("/WEB-INF/classes"+path);
        //获取图片的原名称
        for (MultipartFile file:files) {
            String oriName = file.getOriginalFilename();
            String fileName = UUIDUtil.getUUID2();
            String suffix = oriName.substring(oriName.lastIndexOf('.'));
            String result = fileName + suffix;
            //配置图片上传路径，注意此处的路径和MyMvcConfig.java中配置的路径保持一致
            File uploadPathFile = new File(projectServerRelativelyPath);
            //判断上传的文件夹是否存在
            if (!uploadPathFile.exists()) {
                //不存在则递归创建
                uploadPathFile.mkdirs();
            }
            try {
                // 构建真实的文件路径
                File newFile = new File(projectServerRelativelyPath + File.separator + result);
                System.out.println(newFile.getAbsolutePath());
                // 上传图片到 -》 “绝对路径”
                file.transferTo(newFile);
            } catch (IOException e) {
                e.printStackTrace();
            }
            String fileServerPath = projectServerPath + result;
            filePathSet.put("fileRelativePath",path+result);
            filePathSet.put("fileServerPath",fileServerPath);
            filePathSet.put("fileType",suffix);
            list.add(filePathSet);
        }
        return list;
    }

    public static Boolean deleteUploadFile(String filePath, HttpServletRequest httpServletRequest){
        /**
         * 上传文件绝对路径
         */
        String projectServerRelativelyPath = httpServletRequest.getSession().getServletContext().getRealPath("/WEB-INF/classes");
        File file =new File(projectServerRelativelyPath+filePath);
        if(file.exists()){
            file.delete();
            return true;
        }
        return false;
    }

}
